package top.i51213.fastaction.view;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;

import java.util.ArrayList;
import java.util.Collections;

import top.i51213.fastaction.R;
import top.i51213.fastaction.ReceiveActivity;
import top.i51213.fastaction.ReceiveClassName;
import top.i51213.fastaction.action.image.ImageCompressor;
import top.i51213.fastaction.uri.MimeType;
import top.i51213.fastaction.uri.UriReader;

public class PdfViewerActivity extends AppCompatActivity {
    PDFView pdfView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_viewer);
        String action = getIntent().getAction();
        String type = getIntent().getType();
        Uri uri=getIntent().getData();
        if(!"application/pdf".equalsIgnoreCase(type)||uri==null) {
            Toast.makeText(this,"未加载任何PDF文件",Toast.LENGTH_SHORT).show();
            return;
        }
        loadUri(uri);
    }
    private void loadUri(Uri uri){
        pdfView=findViewById(R.id.pdfView);
        pdfView.fromUri(uri).load();
        pdfView.setTag(uri);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_pdf_view, menu);
        return true;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_action_get_image_400:
                getImagesAndShare(ImageCompressor.LimitRectSize.PX400);
                break;
            case R.id.menu_action_get_image_800:
                getImagesAndShare(ImageCompressor.LimitRectSize.PX800);
                break;
            case R.id.menu_action_get_image_1200:
                getImagesAndShare(ImageCompressor.LimitRectSize.PX1200);
                break;
            case R.id.menu_action_get_long_image:
                getLongImageAndShare(ImageCompressor.LimitRectSize.PX400);
                break;
            case R.id.menu_action_get_pdf:
                ArrayList<Uri> uris=new ArrayList<>(Collections.singletonList((Uri) pdfView.getTag()));
                ReceiveActivity.startThis(this,ReceiveClassName.PdfShare,uris,MimeType.Application.Pdf.get());
                break;
            default:break;
        }

        return super.onOptionsItemSelected(item);
    }
    private void getImagesAndShare(ImageCompressor.LimitRectSize limitRectSize){
        if(pdfView.getTag()==null)return;
        Uri uri=(Uri)pdfView.getTag();
        UriReader uriReader=new UriReader(this,uri);
        ArrayList<Uri> list= ImageCompressor.pdf2Jpeg2(uriReader, limitRectSize);
        ReceiveActivity.startThis(this, ReceiveClassName.ImageShare, list, MimeType.Image.Jpeg.get());
    }
    private void getLongImageAndShare(ImageCompressor.LimitRectSize limitRectSize){
        Uri uri=(Uri)pdfView.getTag();
        UriReader uriReader=new UriReader(this,uri);
        ArrayList<Uri> list= ImageCompressor.pdf2LongPng(uriReader, limitRectSize);
        ReceiveActivity.startThis(this, ReceiveClassName.ImageShare, list, MimeType.Image.Png.get());
    }
}